package com.youlai.boot.factory.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.factory.mapper.WorkOrderProcessMapper;
import com.youlai.boot.factory.service.WorkOrderProcessService;
import com.youlai.boot.factory.model.entity.WorkOrderProcess;
import com.youlai.boot.factory.model.form.WorkOrderProcessForm;
import com.youlai.boot.factory.model.query.WorkOrderProcessQuery;
import com.youlai.boot.factory.model.vo.WorkOrderProcessVO;
import com.youlai.boot.factory.converter.WorkOrderProcessConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 报工工序明细服务实现类
 *
 * @author youlaitech
 * @since 2025-10-16 15:34
 */
@Service
@RequiredArgsConstructor
public class WorkOrderProcessServiceImpl extends ServiceImpl<WorkOrderProcessMapper, WorkOrderProcess> implements WorkOrderProcessService {

    private final WorkOrderProcessConverter workOrderProcessConverter;

    /**
    * 获取报工工序明细分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<WorkOrderProcessVO>} 报工工序明细分页列表
    */
    @Override
    public IPage<WorkOrderProcessVO> getWorkOrderProcessPage(WorkOrderProcessQuery queryParams) {
        Page<WorkOrderProcessVO> pageVO = this.baseMapper.getWorkOrderProcessPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取报工工序明细表单数据
     *
     * @param id 报工工序明细ID
     * @return 报工工序明细表单数据
     */
    @Override
    public WorkOrderProcessForm getWorkOrderProcessFormData(Long id) {
        WorkOrderProcess entity = this.getById(id);
        return workOrderProcessConverter.toForm(entity);
    }
    
    /**
     * 新增报工工序明细
     *
     * @param formData 报工工序明细表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveWorkOrderProcess(WorkOrderProcessForm formData) {
        WorkOrderProcess entity = workOrderProcessConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新报工工序明细
     *
     * @param id   报工工序明细ID
     * @param formData 报工工序明细表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateWorkOrderProcess(Long id,WorkOrderProcessForm formData) {
        WorkOrderProcess entity = workOrderProcessConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除报工工序明细
     *
     * @param ids 报工工序明细ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteWorkOrderProcesss(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的报工工序明细数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
